﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace WPFRadio
{

    [Serializable]
    public class playlist
    {
        private List<Radio> radios;

        public List<Radio> Radios
        {
            get
            {
                if (this.radios == null)
                {
                    this.radios = new List<Radio>();
                }
                return this.radios;
            }
        }

        public void AddRadio(Radio radio)
        {
            if ((!this.Radios.Contains(radio)))
            {
                this.Radios.Add(radio);
            }
        }

        public void savePlayList()
        {
            if (this.Radios.Count == 0)
            {
                return;
            }

           this.Save();
        }

        public override string ToString()
        {
            StringBuilder strBuilder = new StringBuilder();
            strBuilder.AppendFormat("Radios: {0}\n", this.Radios.Count.ToString());
            foreach (var item in this.Radios)
            {
                strBuilder.AppendLine(item.ToString());
            }
            return strBuilder.ToString();
        }
        public static playlist LoadPlaylist()
        {
            var playlist = new playlist();

            return playlist.Load();
            
        }

        private void Save()
        {
           PlaylistSerializer.SerializeToXml<playlist>(this, "playlist.xml");

        }

        private playlist Load()
        {
            if (!System.IO.File.Exists("playlist.xml"))
            {
                return null;
            }

            return PlaylistSerializer.DeserializeFromXml<playlist>("playlist.xml");


        }




    }
}
